package com.adacms.actions.days;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adacms.common.action.BaseAction;
import com.adacms.common.utils.Utils;
import com.adacms.data.entity.DaysUser;
import com.adacms.data.service.DaysUserService;
import com.by.http.Connection;
import com.by.http.Connection.Method;
import com.by.http.HttpConnection;

@Controller
public class WebDayAction extends BaseAction {

	@RequestMapping(value = "user_login", method = RequestMethod.GET)
	public String user_login(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String result = "app/user_login";
		return result;
	}
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String result = "app/user_login";
		
		return result;
	}
	@RequestMapping(value = "user_login_work", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String user_login_work(String phonenum, String password,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String result = "app/index";

		DaysUser user = userService.login(phonenum, password);
		if (user != null) {
			result = "app/index";

		} else {
			result = "app/user_login";
		}
		return result;
	}

	@Autowired
	DaysUserService userService;

	@RequestMapping(value = "user_reg_work", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String user_reg_work(DaysUser user, String code,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String codeinfo = getCode(request);
		if (code != null && code.equals(codeinfo)) {
			String result = "app/index";
			if (userService.checkphone(user.getPassword()) == null) {
				userService.save(user);
				result = "app/index";
			} else {
				model.addAttribute("msg", "该手机号注册过了");
				result = "app/user_reg1";
			}
			return result;

		} else {
			String result = "app/user_reg1";
			model.addAttribute("msg", "验证码错误");
			return result;
		}

	}

	@RequestMapping(value = "user_reg1", method = RequestMethod.GET)
	public String user_reg1(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String result = "app/user_reg1";
		return result;
	}

	@RequestMapping(value = "user_reg", method = RequestMethod.GET)
	public String user_reg(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String result = "app/user_reg";
		return result;
	}

	public static void main(String[] args) {
		System.out.println(URLEncoder.encode("七天合伙人"));
	}

	@RequestMapping(value = "checkphone", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String checkphone(String phonenum, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		if (userService.checkphone(phonenum) != null) {
			model.addAttribute("msg", "该手机号注册过了");
			return "app/user_reg";
		}

		String result = "app/user_reg1";
		String num = Utils.getnumpassword(4);
		String url = "http://apis.haoservice.com/sms/send";
		String vc = "#code#=%s&#company#=七天合伙人";

		Connection con = HttpConnection.connect(url).method(Method.POST);
		con.data("mobile", phonenum);
		con.data("key", "97832bbf35f746efa35cd23b47799389");
		con.data("dtype", "json");
		con.data("tpl_id", "2");
		con.data("tpl_value", String.format(vc, num));
		try {
			String body = con.execute().body();
			if (body.indexOf("Success") > 0) {

				result = "app/user_reg1";
				setMember(request, num);
			} else {
				result = "app/user_reg";
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	@RequestMapping(value = "senphonephone", method = { RequestMethod.GET,
			RequestMethod.POST })
	public @ResponseBody String senphonephone(String phonenum,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String result = "ok";
		String num = Utils.getnumpassword(4);

		String url = "http://apis.haoservice.com/sms/send";
		String vc = "#code#=%s&#company#=七天合伙人";

		Connection con = HttpConnection.connect(url).method(Method.POST);
		con.data("mobile", phonenum);
		con.data("key", "97832bbf35f746efa35cd23b47799389");
		con.data("dtype", "json");
		con.data("tpl_id", "2");
		con.data("tpl_value", String.format(vc, num));
		try {
			String body = con.execute().body();
			if (body.indexOf("Success") > 0) {
				result = "ok";
				setMember(request, num);

			} else {
				result = "fail";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
